Imports System
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports System.Configuration
Imports System.Web.UI.WebControls
Imports System.Text
Public Class findusers
    Inherits System.Web.UI.Page
    Public Msg, target, sortby, sPin As String
    Public aUserSession(13) As String
    Public PG As Integer
    Public bLocked As Boolean
    Public nTempRPP, i As Integer
    Public ToggleSortOrder As Boolean
    Public Const cSESSION_ID = 0
    Public Const cCOMPANY_ID = 1
    Public Const cGAPORTAL_ID = 2
    Public Const cUSER_ID = 3
    Public Const cINCIDCONTEXT = 4
    Public Const cINGIDCONTEXT = 5
    Public Const cLOGINDATETIME = 6
    Public Const cLASTACTIVITY = 7
    Public Const cUSERPERMISSIONSSTRING = 8
    'Public Const cTERMINAL_PIN = 9
    Public Const cROLE_ID = 9
    Public Const cMEMBER_TYPE = 10
    'Public Const cTERMINAL_NAME = 10
    Public Const cTERMINAL_NUMBER = 11
    Public Const cTERMINAL_MARKET = 12
    Public svid, sUID, CID, CompanyID As String
    Public sQS, HTMLString, HTMLDispage, HTMLMertList As String
    Public nNumPages, FilteredPG As Integer
    Protected WithEvents vid As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFname As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtLname As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtLoc As System.Web.UI.WebControls.TextBox
    Protected WithEvents RPP As System.Web.UI.WebControls.DropDownList
    Protected WithEvents btnFind As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Public vRs As DataSet
    Public objvalid As New gaValidation
    Public FilteredUID As String
    Public FilteredCID As String
    Public FilteredVID As String
    Public FilteredFirstname As String
    Public Filteredlastname As String
    Public Filteredlocation As String
    Public FilteredRPP As String
    Public Filteredsortby As String
    'Public objvalid As New gaValidation
    Public imgpath, currtURL As String
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        objvalid.getURL(Request.Url.ToString, imgpath, currtURL)
        BuildRPP()
    End Sub

    Private Sub BuildRPP()
        Dim i As Integer
        ''RPP.Items.Add(New ListItem("M", "115"))
        If Not IsPostBack Then
            nTempRPP = 0
            RPP.Items.Clear()
            For i = 1 To 4
                RPP.Items.Add(New ListItem(i * 25, i))
            Next
            RPP.SelectedIndex = 1
        Else
            If CInt(Request("RPP")) > 0 Then
                nTempRPP = CInt(Request("RPP"))
            End If
            For i = 1 To 4 Step 1
                If nTempRPP = i * 25 Then
                    RPP.SelectedItem.Text = nTempRPP
                    Exit For
                End If
            Next
        End If
        '<tr>
        '		<td>Records Per Page :
        '		</td>
        '		<td>
        '			<select name="RPP">
        '<%			nTempRPP = 0
        '        If Request("RPP").count > 0 Then
        '            nTempRPP = CInt(Request("RPP"))
        '        End If
        '        For i = 1 To 4 Step 1
        '				if nTempRPP = i * 25 then %>
        '					<option selected><%=i*25%>
        '		 <%	else %>
        '				<option><%=i*25%>
        '		 <%	end if
        '			next %>
        '				</select>
        '				</td>
        '			</tr>

    End Sub

    Public Function GetSearchResults(ByVal vRs As DataSet) As String
        Dim colctr As Integer
        Dim fname, lname, location, supervisor, active As String
        Dim compname
        For colctr = 0 To vRs.Tables(0).Rows.Count - 1
            compname = vRs.Tables(0).Rows(colctr).Item(0) '(0, colctr)
            sUID = vRs.Tables(0).Rows(colctr).Item(1) 'vRs(1, colctr)
            CID = vRs.Tables(0).Rows(colctr).Item(2) 'vRs(2, colctr)
            svid = vRs.Tables(0).Rows(colctr).Item(3)
            fname = vRs.Tables(0).Rows(colctr).Item(4) ''vRs(4, colctr)
            lname = vRs.Tables(0).Rows(colctr).Item(5) ''vRs(5, colctr)
            location = vRs.Tables(0).Rows(colctr).Item(6) ''vRs(6, colctr)
            supervisor = vRs.Tables(0).Rows(colctr).Item(7) ''vRs(7, colctr)
            active = vRs.Tables(0).Rows(colctr).Item(8) 'vRs(8, colctr)
            HTMLString = HTMLString & vbCr & vbTab & vbTab & "<tr bgcolor=""" & Application("AppNavTextBGColor") & """>"
            If aUserSession(cCOMPANY_ID) = "0" Then
                HTMLString += "<font face=""arial"" size=""2""><a href=""javascript:userdetail('" & sUID & "','" & CID & "','" & svid & "')"">" & sUID & "</a></font>"
            Else
                HTMLString = HTMLString & "<td valign=""top""><a href=""javascript:userdetail('" & sUID & "',' ','" & svid & "')"">" & sUID & "</a>&nbsp;</td>"
            End If
            If aUserSession(cCOMPANY_ID) = "0" Then
                HTMLString = HTMLString & "<td valign=""top"">" & compname & "&nbsp;</td>"
            End If

            HTMLString = HTMLString & "<td valign=""top"">" & svid & "&nbsp;</td>"

            HTMLString = HTMLString & "<td valign=""top"">" & fname & "&nbsp;</td>"
            HTMLString = HTMLString & "<td valign=""top"">" & lname & "&nbsp;</td>"
            HTMLString = HTMLString & "<td valign=""top"">" & location & "&nbsp;</td>"
            HTMLString = HTMLString & "<td valign=""top"">" & supervisor & "&nbsp;</td>"
            HTMLString = HTMLString & "<td valign=""top"">" & UCase(active) & "&nbsp;</td>"

        Next
        HTMLString = HTMLString & "</tr>"
        Return HTMLString
    End Function
    'Public Function DrawPaginationControls() As String
    '    Dim strPageNum As String
    '    'If 'Request("sortby").count > 0 Then
    '    'sQS = sQS & "&sortby=" & Request("sortby")
    '    'End If
    '    strPageNum = "<table class=""pos""><tr>"

    '    If FilteredPG > 1 Then
    '        strPageNum += "<td align=""left""><font face=""arial"" size=""2"">"
    '        strPageNum += "	<a href=""adm_findusers.asp<%=sQS%>&action=Refind&PG=<%=FilteredPG - 1%>""><b>PREVIOUS PAGE</b></a></font></td>"

    '    End If
    '    strPageNum += "<td><font face=""arial"" size=2>&nbsp;</font></td><td>"

    '    strPageNum += "<font face=""arial"" size=2>Displaying Page <%=FilteredPG%> of <%=nNumPages%>.</font></td><td><font face=""arial"" size=2>&nbsp;</font></td>"

    '    If nNumPages > FilteredPG Then
    '        strPageNum += "<td align=""right"">"
    '        strPageNum += "	<font face=""arial"" size=""2""><a href=""adm_findusers.asp<%=sQS%>&action=Refind&PG=<%=FilteredPG + 1%>""><b>NEXT PAGE</b></a></font></td>"


    '    End If
    '    strPageNum += "</tr></table>"
    '    HTMLDispage = strPageNum
    '    Return HTMLDispage
    'End Function
    Public Sub DisplayPaginationControls()
        If nNumPages > 0 Then

            HTMLDispage = "<table border='0' cellspacing='0' cellpadding='0' align='center' width='100%'><tr><td align='right' width='50%'>"

            If FilteredPG > 1 Then
                HTMLDispage += "<a href='adm_findusers.asp<%=sQS%>&action=Refind&PG=<%=FilteredPG - 1%>'><b>PREVIOUS PAGE</b></a>"
            Else
                HTMLDispage += "&nbsp;"
            End If
            HTMLDispage += "</td><td>&nbsp;</td><td nowrap='true'>Displaying Page <%=FilteredPG%> of <%=nNumPages%>.</td><td>&nbsp;</td><td align='left' width='50%'>"


            If IsArray(vRs) And nNumPages > FilteredPG Then
                HTMLDispage += "<a href='findusers.aspx<%=sQS%>&action=Refind&PG=<%=FilteredPG + 1%>'><b>NEXT PAGE</b></a>"
            Else
                HTMLDispage += "&nbsp;"
            End If
            HTMLDispage += "</td></tr></table>"

        End If
    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        '       If Request("action") = "Find" Or Request("action") = "Refind" Then
        Dim ofinduser As UserManagement.UserManagement
        ToggleSortOrder = False

        FilteredUID = Trim(Request("sUID"))
        FilteredCID = Trim(Request("sCID"))
        FilteredVID = Trim(vid.Text)
        FilteredFirstname = Trim(txtFname.Text)
        Filteredlastname = Trim(txtLname.Text)
        Filteredlocation = Trim(txtLoc.Text)
        FilteredRPP = CInt(Request("RPP"))
        FilteredPG = CInt(Request("PG"))
        Filteredsortby = Trim(Request("sortby"))

        sQS = "?sUID=" & Server.UrlEncode(FilteredUID)
        If aUserSession(cCOMPANY_ID) = "0" Then
            sQS = sQS & "&sCID=" & Server.UrlEncode(FilteredCID)
        End If
        sQS = sQS & "&sVID=" & Server.UrlEncode(FilteredVID)
        sQS = sQS & "&firstname=" & Server.UrlEncode(FilteredFirstname)
        sQS = sQS & "&lastname=" & Server.UrlEncode(Filteredlastname)
        sQS = sQS & "&location=" & Server.UrlEncode(Filteredlocation)
        sQS = sQS & "&RPP=" & Server.UrlEncode(FilteredRPP)

        ofinduser = New UserManagement.UserManagement
        ofinduser.DBConnectstring = Application("DSN")
        ofinduser.nPage = FilteredPG
        ofinduser.nPageRecords = FilteredRPP
        If aUserSession(cCOMPANY_ID) = "0" And FilteredCID = "" Then
            FilteredCID = -1
        ElseIf aUserSession(cCOMPANY_ID) = "0" Then
            FilteredCID = CInt(FilteredCID)
        Else
            FilteredCID = CInt(Application("CompanyID"))
        End If
        ''==search subordinates for employee user
        'If TheHeader1.isELevelUser(aUserSession(cVIAKLIX_ID), aUserSession(cUSER_ID)) Then
        '    ofinduser.SESSION_USER_ID = aUserSession(cUSER_ID)
        '    ofinduser.SESSION_VIAKLIX_ID = aUserSession(cVIAKLIX_ID)
        '    ofinduser.isELevelUser = True
        'End If
        'If Request("action") = "Find" Then
        vRs = New DataSet
        ofinduser.SessionID = Session("UserSession")
        vRs = ofinduser.FindUsers(FilteredUID, FilteredCID, FilteredVID, Filteredlastname, FilteredFirstname, Filteredlocation, Filteredsortby, ToggleSortOrder)
        nNumPages = ofinduser.nNumPages
        GetSearchResults(vRs)

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ToggleSortOrder = True

        Response.Redirect("adm_default.aspx")

    End Sub
End Class
